// main.go
// Entry point for the AgentDesign backend server.

package main

import (
	"fmt"
	"log"

	"agentdesign/config"
	"agentdesign/database"
	"agentdesign/handlers"
	"agentdesign/router"
	"agentdesign/service"
)

func main() {
	// Load the configuration from the environment variables.
	config, err := config.LoadConfig()
	if err != nil {
		log.Fatalf("failed to load configuration: %v", err)
	}

	connection, err := database.InitializeMySQL(&config.Database)

	if err != nil {
		log.Fatalf("\nfailed to initialize MySQL: %v", err)
	}

	defer connection.Close()

	serviceInstance := service.SetApiService(connection)
	promptService := service.NewPromptServiceFromEnv()

	handlerInstance := handlers.SetHandler(serviceInstance, promptService)

	// Initialize the Gin router using our custom router setup.
	r := router.SetupRouter(handlerInstance)
	// Run the server on port 9980.  If binding fails the application will log
	// the error and exit.  By default Gin listens on 0.0.0.0.
	if err := r.Run(fmt.Sprintf(":%d", config.Port)); err != nil {
		log.Fatalf("failed to run server: %v", err)
	}
}
